/*
 * GuudCanvas.h
 *
 *  Created on: 2013-1-23
 *      Author: heqiu
 */

#ifndef GUUDCANVAS_H_
#define GUUDCANVAS_H_

#include "GuudPoint.h"
#include "GuudListener.h"

namespace guud {

class GuudCanvas {
public:
	class CanvasMovingEvent {
	public:
		CanvasMovingEvent() {
			m_bMoveing = false;
			m_iLastXPixel = 0;
			m_iLastYPixel = 0;
			m_bListen = true;
		}
		bool m_bListen;
		bool m_bMoveing;
		int32_t m_iLastXPixel;
		int32_t m_iLastYPixel;
		void handleEvent(GuudCanvas * pGuudCanvas, GuudEvent &stEvent) {
			if (!m_bListen) {
				return;
			}
			int32_t iX = stEvent.motion.x;
			int32_t iY = stEvent.motion.y;
			if (stEvent.type == SDL_MOUSEMOTION) {
				if (m_bMoveing) {
					pGuudCanvas->m_gpScreenPosPixel.m_iX -=
							(iX - m_iLastXPixel);
					pGuudCanvas->m_gpScreenPosPixel.m_iY -=
							(iY - m_iLastYPixel);
					m_iLastXPixel = iX;
					m_iLastYPixel = iY;
				}
			} else if (stEvent.type == SDL_MOUSEBUTTONDOWN) {
				m_bMoveing = true;
				m_iLastXPixel = iX;
				m_iLastYPixel = iY;
			} else if (stEvent.type == SDL_MOUSEBUTTONUP) {
				m_bMoveing = false;
			}
		}
	};
	bool m_bShow;
	bool m_bListen;
	uint32_t m_uiShortUnit;
	uint32_t m_uiLongUnit;
	uint32_t m_uiUnitPixel;
	GuudPoint m_gpScreenPosPixel;	//canvas相对于 screen 的像素坐标

	//画布的大小，(短轴，长轴， (s,0)(l,0)(0,s)(0,l) 四点中的矩形为地图大小，每个单元的像素大小
	GuudCanvas(uint32_t uiShortUnit, uint32_t uiLongUnit, uint32_t uiUnitPixel);
	virtual ~GuudCanvas();
	virtual void draw();
	virtual void init();
	virtual void handleEvent(GuudEvent &stEvent);
	//获取四个顶点的坐标
	void getPeak(GuudPoint & gpAUnit, GuudPoint & gpBUnit, GuudPoint & gpCUnit,
			GuudPoint & gpDUnit);
	//判断点是否在地图内部
	bool bUnitIn(GuudPoint & gpUnit);
	//循环所有的地图内部坐标
	void cycleAll(void (*func)(int32_t, int32_t, void *), void * pData);
	//循环可见矩形内的坐标
	void cycleRect(GuudRect & rect,
			void (*func)(GuudPoint & gpUnit, GuudPoint & gpScreen, void *),
			void * pData);
	//地图转换成屏幕坐标
	void transformToScreen(GuudPoint gpUnit, GuudPoint & gpScreen);
	void transformToScreen(GuudFPoint gpUnit, GuudFPoint & gpScreen);
	//屏幕转换成地图坐标
	void transformToUnit(GuudPoint gpScreen, GuudPoint & gpUnit);
	void transformToUnit(GuudFPoint gpScreen, GuudFPoint & gpUnit);
	void transformPixelToUnit(GuudPoint gpPixel, GuudPoint & gpUnit);
	void transformUnitToPixel(GuudPoint gpUnit, GuudPoint & gpPixel);
	int32_t getScreenX(int32_t iXPixel);
	int32_t getScreenY(int32_t iYPixel);
	uint32_t getScreenWidth(uint32_t uiX);
	uint32_t getScreenHeight(uint32_t uiY);
private:
	//不允许拷贝构造
	GuudCanvas(const GuudCanvas & obj);

	GuudPoint m_gpAUnit;	//四个顶点
	GuudPoint m_gpBUnit;
	GuudPoint m_gpCUnit;
	GuudPoint m_gpDUnit;

};

}

#endif /* GUUDCANVAS_H_ */
